<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.4: http://docutils.sourceforge.net/" />
<title>Testing the numpy.i Typemaps</title>
<meta name="author" content="Bill Spotz" />
<meta name="date" content="6 April, 2007" />
<style type="text/css">

/*
:Author: David Goodger
:Contact: goodger@users.sourceforge.net
:Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
:Revision: $Revision: 4224 $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
  border: 0 }

table.borderless td, table.borderless th {
  /* Override padding for "table.docutils td" with "! important".
     The right padding separates the table cells. */
  padding: 0 0.5em 0 0 ! important }

.first {
  /* Override more specific margin styles with "! important". */
  margin-top: 0 ! important }

.last, .with-subtitle {
  margin-bottom: 0 ! important }

.hidden {
  display: none }

a.toc-backref {
  text-decoration: none ;
  color: black }

blockquote.epigraph {
  margin: 2em 5em ; }

dl.docutils dd {
  margin-bottom: 0.5em }

/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
  font-weight: bold }
*/

div.abstract {
  margin: 2em 5em }

div.abstract p.topic-title {
  font-weight: bold ;
  text-align: center }

div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
  margin: 2em ;
  border: medium outset ;
  padding: 1em }

div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
  font-weight: bold ;
  font-family: sans-serif }

div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
  color: red ;
  font-weight: bold ;
  font-family: sans-serif }

/* Uncomment (and remove this text!) to get reduced vertical space in
   compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
  margin-bottom: 0.5em }

div.compound .compound-last, div.compound .compound-middle {
  margin-top: 0.5em }
*/

div.dedication {
  margin: 2em 5em ;
  text-align: center ;
  font-style: italic }

div.dedication p.topic-title {
  font-weight: bold ;
  font-style: normal }

div.figure {
  margin-left: 2em ;
  margin-right: 2em }

div.footer, div.header {
  clear: both;
  font-size: smaller }

div.line-block {
  display: block ;
  margin-top: 1em ;
  margin-bottom: 1em }

div.line-block div.line-block {
  margin-top: 0 ;
  margin-bottom: 0 ;
  margin-left: 1.5em }

div.sidebar {
  margin-left: 1em ;
  border: medium outset ;
  padding: 1em ;
  background-color: #ffffee ;
  width: 40% ;
  float: right ;
  clear: right }

div.sidebar p.rubric {
  font-family: sans-serif ;
  font-size: medium }

div.system-messages {
  margin: 5em }

div.system-messages h1 {
  color: red }

div.system-message {
  border: medium outset ;
  padding: 1em }

div.system-message p.system-message-title {
  color: red ;
  font-weight: bold }

div.topic {
  margin: 2em }

h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  margin-top: 0.4em }

h1.title {
  text-align: center }

h2.subtitle {
  text-align: center }

hr.docutils {
  width: 75% }

img.align-left {
  clear: left }

img.align-right {
  clear: right }

ol.simple, ul.simple {
  margin-bottom: 1em }

ol.arabic {
  list-style: decimal }

ol.loweralpha {
  list-style: lower-alpha }

ol.upperalpha {
  list-style: upper-alpha }

ol.lowerroman {
  list-style: lower-roman }

ol.upperroman {
  list-style: upper-roman }

p.attribution {
  text-align: right ;
  margin-left: 50% }

p.caption {
  font-style: italic }

p.credits {
  font-style: italic ;
  font-size: smaller }

p.label {
  white-space: nowrap }

p.rubric {
  font-weight: bold ;
  font-size: larger ;
  color: maroon ;
  text-align: center }

p.sidebar-title {
  font-family: sans-serif ;
  font-weight: bold ;
  font-size: larger }

p.sidebar-subtitle {
  font-family: sans-serif ;
  font-weight: bold }

p.topic-title {
  font-weight: bold }

pre.address {
  margin-bottom: 0 ;
  margin-top: 0 ;
  font-family: serif ;
  font-size: 100% }

pre.literal-block, pre.doctest-block {
  margin-left: 2em ;
  margin-right: 2em ;
  background-color: #eeeeee }

span.classifier {
  font-family: sans-serif ;
  font-style: oblique }

span.classifier-delimiter {
  font-family: sans-serif ;
  font-weight: bold }

span.interpreted {
  font-family: sans-serif }

span.option {
  white-space: nowrap }

span.pre {
  white-space: pre }

span.problematic {
  color: red }

span.section-subtitle {
  /* font-size relative to parent (h1..h6 element) */
  font-size: 80% }

table.citation {
  border-left: solid 1px gray;
  margin-left: 1px }

table.docinfo {
  margin: 2em 4em }

table.docutils {
  margin-top: 0.5em ;
  margin-bottom: 0.5em }

table.footnote {
  border-left: solid 1px black;
  margin-left: 1px }

table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
  padding-left: 0.5em ;
  padding-right: 0.5em ;
  vertical-align: top }

table.docutils th.field-name, table.docinfo th.docinfo-name {
  font-weight: bold ;
  text-align: left ;
  white-space: nowrap ;
  padding-left: 0 }

h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  font-size: 100% }

tt.docutils {
  background-color: #eeeeee }

ul.auto-toc {
  list-style-type: none }

</style>
</head>
<body>
<div class="document" id="testing-the-numpy-i-typemaps">
<h1 class="title">Testing the numpy.i Typemaps</h1>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Bill Spotz</td></tr>
<tr class="field"><th class="docinfo-name">Institution:</th><td class="field-body">Sandia National Laboratories</td>
</tr>
<tr><th class="docinfo-name">Date:</th>
<td>6 April, 2007</td></tr>
</tbody>
</table>
<div class="contents topic">
<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
<ul class="simple">
<li><a class="reference" href="#introduction" id="id1" name="id1">Introduction</a></li>
<li><a class="reference" href="#testing-organization" id="id2" name="id2">Testing Organization</a></li>
<li><a class="reference" href="#testing-header-files" id="id3" name="id3">Testing Header Files</a></li>
<li><a class="reference" href="#testing-source-files" id="id4" name="id4">Testing Source Files</a></li>
<li><a class="reference" href="#testing-swig-interface-files" id="id5" name="id5">Testing SWIG Interface Files</a></li>
<li><a class="reference" href="#testing-python-scripts" id="id6" name="id6">Testing Python Scripts</a></li>
</ul>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id1" id="introduction" name="introduction">Introduction</a></h1>
<p>Writing tests for the <tt class="docutils literal"><span class="pre">numpy.i</span></tt> <a class="reference" href="http://www.swig.org">SWIG</a>
interface file is a combinatorial headache.  At present, 12 different
data types are supported, each with 23 different argument signatures,
for a total of 276 typemaps supported &quot;out of the box&quot;.  Each of these
typemaps, in turn, might require several unit tests in order to verify
expected behavior for both proper and improper inputs.  Currently,
this results in 1,020 individual unit tests that are performed when
<tt class="docutils literal"><span class="pre">make</span> <span class="pre">test</span></tt> is run in the <tt class="docutils literal"><span class="pre">numpy/docs/swig</span></tt> subdirectory.</p>
<p>To facilitate this many similar unit tests, some high-level
programming techniques are employed, including C and <a class="reference" href="http://www.swig.org">SWIG</a> macros,
as well as  <a class="reference" href="http://www.python.org">python</a> inheritance.  The
purpose of this document is to describe the testing infrastructure
employed to verify that the <tt class="docutils literal"><span class="pre">numpy.i</span></tt> typemaps are working as
expected.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id2" id="testing-organization" name="testing-organization">Testing Organization</a></h1>
<p>There are three indepedent testing frameworks supported, for one-,
two-, and three-dimensional arrays respectively.  For one-dimensional
arrays, there are two C++ files, a header and a source, named:</p>
<pre class="literal-block">
Vector.h
Vector.cxx
</pre>
<p>that contain prototypes and code for a variety of functions that have
one-dimensional arrays as function arguments.  The file:</p>
<pre class="literal-block">
Vector.i
</pre>
<p>is a <a class="reference" href="http://www.swig.org">SWIG</a> interface file that defines a python module <tt class="docutils literal"><span class="pre">Vector</span></tt>
that wraps the functions in <tt class="docutils literal"><span class="pre">Vector.h</span></tt> while utilizing the typemaps
in <tt class="docutils literal"><span class="pre">numpy.i</span></tt> to correctly handle the C arrays.</p>
<p>The <tt class="docutils literal"><span class="pre">Makefile</span></tt> calls <tt class="docutils literal"><span class="pre">swig</span></tt> to generate <tt class="docutils literal"><span class="pre">Vector.py</span></tt> and
<tt class="docutils literal"><span class="pre">Vector_wrap.cxx</span></tt>, and also executes the <tt class="docutils literal"><span class="pre">setup.py</span></tt> script that
compiles <tt class="docutils literal"><span class="pre">Vector_wrap.cxx</span></tt> and links together the extension module
<tt class="docutils literal"><span class="pre">_Vector.so</span></tt> or <tt class="docutils literal"><span class="pre">_Vector.dylib</span></tt>, depending on the platform.  This
extension module and the proxy file <tt class="docutils literal"><span class="pre">Vector.py</span></tt> are both placed in a
subdirectory under the <tt class="docutils literal"><span class="pre">build</span></tt> directory.</p>
<p>The actual testing takes place with a <a class="reference" href="http://www.python.org">python</a> script named:</p>
<pre class="literal-block">
testVector.py
</pre>
<p>that uses the standard <a class="reference" href="http://www.python.org">python</a> library module <tt class="docutils literal"><span class="pre">unittest</span></tt>, which
performs several tests of each function defined in <tt class="docutils literal"><span class="pre">Vector.h</span></tt> for
each data type supported.</p>
<p>Two-dimensional arrays are tested in exactly the same manner.  The
above description applies, but with <tt class="docutils literal"><span class="pre">Matrix</span></tt> substituted for
<tt class="docutils literal"><span class="pre">Vector</span></tt>.  For three-dimensional tests, substitute <tt class="docutils literal"><span class="pre">Tensor</span></tt> for
<tt class="docutils literal"><span class="pre">Vector</span></tt>.  For the descriptions that follow, we will reference the
<tt class="docutils literal"><span class="pre">Vector</span></tt> tests, but the same information applies to <tt class="docutils literal"><span class="pre">Matrix</span></tt> and
<tt class="docutils literal"><span class="pre">Tensor</span></tt> tests.</p>
<p>The command <tt class="docutils literal"><span class="pre">make</span> <span class="pre">test</span></tt> will ensure that all of the test software is
built and then run all three test scripts.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id3" id="testing-header-files" name="testing-header-files">Testing Header Files</a></h1>
<p><tt class="docutils literal"><span class="pre">Vector.h</span></tt> is a C++ header file that defines a C macro called
<tt class="docutils literal"><span class="pre">TEST_FUNC_PROTOS</span></tt> that takes two arguments: <tt class="docutils literal"><span class="pre">TYPE</span></tt>, which is a
data type name such as <tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">int</span></tt>; and <tt class="docutils literal"><span class="pre">SNAME</span></tt>, which is a
short name for the same data type with no spaces, e.g. <tt class="docutils literal"><span class="pre">uint</span></tt>.  This
macro defines several function prototypes that have the prefix
<tt class="docutils literal"><span class="pre">SNAME</span></tt> and have at least one argument that is an array of type
<tt class="docutils literal"><span class="pre">TYPE</span></tt>.  Those functions that have return arguments return a
<tt class="docutils literal"><span class="pre">TYPE</span></tt> value.</p>
<p><tt class="docutils literal"><span class="pre">TEST_FUNC_PROTOS</span></tt> is then implemented for all of the data types
supported by <tt class="docutils literal"><span class="pre">numpy.i</span></tt>:</p>
<blockquote>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">signed</span> <span class="pre">char</span></tt></li>
<li><tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">char</span></tt></li>
<li><tt class="docutils literal"><span class="pre">short</span></tt></li>
<li><tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">short</span></tt></li>
<li><tt class="docutils literal"><span class="pre">int</span></tt></li>
<li><tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">int</span></tt></li>
<li><tt class="docutils literal"><span class="pre">long</span></tt></li>
<li><tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span></tt></li>
<li><tt class="docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt></li>
<li><tt class="docutils literal"><span class="pre">unsigned</span> <span class="pre">long</span> <span class="pre">long</span></tt></li>
<li><tt class="docutils literal"><span class="pre">float</span></tt></li>
<li><tt class="docutils literal"><span class="pre">double</span></tt></li>
</ul>
</blockquote>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id4" id="testing-source-files" name="testing-source-files">Testing Source Files</a></h1>
<p><tt class="docutils literal"><span class="pre">Vector.cxx</span></tt> is a C++ source file that implements compilable code
for each of the function prototypes specified in <tt class="docutils literal"><span class="pre">Vector.h</span></tt>.  It
defines a C macro <tt class="docutils literal"><span class="pre">TEST_FUNCS</span></tt> that has the same arguments and works
in the same way as <tt class="docutils literal"><span class="pre">TEST_FUNC_PROTOS</span></tt> does in <tt class="docutils literal"><span class="pre">Vector.h</span></tt>.
<tt class="docutils literal"><span class="pre">TEST_FUNCS</span></tt> is implemented for each of the 12 data types as above.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id5" id="testing-swig-interface-files" name="testing-swig-interface-files">Testing SWIG Interface Files</a></h1>
<p><tt class="docutils literal"><span class="pre">Vector.i</span></tt> is a <a class="reference" href="http://www.swig.org">SWIG</a> interface file that defines python module
<tt class="docutils literal"><span class="pre">Vector</span></tt>.  It follows the conventions for using <tt class="docutils literal"><span class="pre">numpy.i</span></tt> as
described in the <a class="reference" href="numpy_swig.html">numpy.i documentation</a>.  It
defines a <a class="reference" href="http://www.swig.org">SWIG</a> macro <tt class="docutils literal"><span class="pre">%apply_numpy_typemaps</span></tt> that has a single
argument <tt class="docutils literal"><span class="pre">TYPE</span></tt>.  It uses the <a class="reference" href="http://www.swig.org">SWIG</a> directive <tt class="docutils literal"><span class="pre">%apply</span></tt> as
described in the <a class="reference" href="numpy_swig.html">numpy.i documentation</a> to apply the provided
typemaps to the argument signatures found in <tt class="docutils literal"><span class="pre">Vector.h</span></tt>.  This macro
is then implemented for all of the data types supported by
<tt class="docutils literal"><span class="pre">numpy.i</span></tt>.  It then does a <tt class="docutils literal"><span class="pre">%include</span> <span class="pre">&quot;Vector.h&quot;</span></tt> to wrap all of
the function prototypes in <tt class="docutils literal"><span class="pre">Vector.h</span></tt> using the typemaps in
<tt class="docutils literal"><span class="pre">numpy.i</span></tt>.</p>
</div>
<div class="section">
<h1><a class="toc-backref" href="#id6" id="testing-python-scripts" name="testing-python-scripts">Testing Python Scripts</a></h1>
<p>After <tt class="docutils literal"><span class="pre">make</span></tt> is used to build the testing extension modules,
<tt class="docutils literal"><span class="pre">testVector.py</span></tt> can be run to execute the tests.  As with other
scripts that use <tt class="docutils literal"><span class="pre">unittest</span></tt> to facilitate unit testing,
<tt class="docutils literal"><span class="pre">testVector.py</span></tt> defines a class that inherits from
<tt class="docutils literal"><span class="pre">unittest.TestCase</span></tt>:</p>
<pre class="literal-block">
class VectorTestCase(unittest.TestCase):
</pre>
<p>However, this class is not run directly.  Rather, it serves as a base
class to several other python classes, each one specific to a
particular data type.  The <tt class="docutils literal"><span class="pre">VectorTestCase</span></tt> class stores two strings
for typing information:</p>
<blockquote>
<dl class="docutils">
<dt><strong>self.typeStr</strong></dt>
<dd>A string that matches one of the <tt class="docutils literal"><span class="pre">SNAME</span></tt> prefixes used in
<tt class="docutils literal"><span class="pre">Vector.h</span></tt> and <tt class="docutils literal"><span class="pre">Vector.cxx</span></tt>.  For example, <tt class="docutils literal"><span class="pre">&quot;double&quot;</span></tt>.</dd>
<dt><strong>self.typeCode</strong></dt>
<dd>A short (typically single-character) string that represents a
data type in numpy and corresponds to <tt class="docutils literal"><span class="pre">self.typeStr</span></tt>.  For
example, if <tt class="docutils literal"><span class="pre">self.typeStr</span></tt> is <tt class="docutils literal"><span class="pre">&quot;double&quot;</span></tt>, then
<tt class="docutils literal"><span class="pre">self.typeCode</span></tt> should be <tt class="docutils literal"><span class="pre">&quot;d&quot;</span></tt>.</dd>
</dl>
</blockquote>
<p>Each test defined by the <tt class="docutils literal"><span class="pre">VectorTestCase</span></tt> class extracts the python
function it is trying to test by accessing the <tt class="docutils literal"><span class="pre">Vector</span></tt> module's
dictionary:</p>
<pre class="literal-block">
length = Vector.__dict__[self.typeStr + &quot;Length&quot;]
</pre>
<p>In the case of double precision tests, this will return the python
function <tt class="docutils literal"><span class="pre">Vector.doubleLength</span></tt>.</p>
<p>We then define a new test case class for each supported data type with
a short definition such as:</p>
<pre class="literal-block">
class doubleTestCase(VectorTestCase):
    def __init__(self, methodName=&quot;runTest&quot;):
        VectorTestCase.__init__(self, methodName)
        self.typeStr  = &quot;double&quot;
        self.typeCode = &quot;d&quot;
</pre>
<p>Each of these 12 classes is collected into a <tt class="docutils literal"><span class="pre">unittest.TestSuite</span></tt>,
which is then executed.  Errors and failures are summed together and
returned as the exit argument.  Any non-zero result indicates that at
least one test did not pass.</p>
</div>
</div>
<div class="footer">
<hr class="footer" />
Generated on: 2007-04-06 21:21 UTC.
Generated by <a class="reference" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.

</div>
</body>
</html>
